package com.example.workflow.listener;

import com.example.workflow.pojo.UserInfo;
import com.example.workflow.service.UserInfoService;
import com.example.workflow.util.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.BaseDelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateListener;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service("returnRemindDelegateListener")
public class ReturnRemindDelegateListener implements JavaDelegate {


    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MailUtils mailUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(DelegateExecution delegateExecution) throws Exception {
        String processInstanceId = delegateExecution.getProcessInstanceId();
        String businessKey = delegateExecution.getProcessBusinessKey();
        UserInfo userInfo = userInfoService.getUserInfoByOrder(Long.getLong(businessKey));
        log.info("退回并通知发起人 {}",userInfo.getId());
//        TODO 给创造者发送消息
//        mailUtils.sendMail(userInfo.getEmail(),null,null,"工单退回通知","尊敬的用户，您好：\n您申请的工单流程审批未通过，已退回，请登录系统查阅！");
    }
}
